// POST parameter serialization
// import {Indicator} from 'mint-ui'

import {Toast} from 'vant'
import store from '@/repo/store/store'

const addApi = (instance) => {

  instance.interceptors.request.use(
    config => {
      if (config.method === 'post') {
        let obj = config.data || {}
        if (!obj.hasOwnProperty('token')) {
          obj.sessionToken = store.getters.token || ''
        }
        obj.userid = obj.userid || store.getters.userid || '0'
        obj.tsId = obj.tsId || store.getters.hsId

        config.data = obj
      }
      return config
    },
    error => {
      return Promise.reject(error)
    }
  )
  instance.interceptors.response.use(
    res => res,
    error => {
      Toast('请求超时')
      return Promise.reject(error)
    }
  )
}

const addNewApi = (instance) => {



  instance.interceptors.response.use(
    res => {
      return res
    },
    error => {
      Toast('请求超时')
      return Promise.reject(error)
    }
  )
}
const addTokenToHeader = (instance) => {
  instance.interceptors.request.use(
    config => {
      let obj = config.data || {}
      if (obj.hasOwnProperty('token')) {
        config.headers.token = obj.token
      } else {
        config.headers.token = store.getters.token || ''
      }
      return config
    },
    error => {
      return Promise.reject(error)
    }
  )
}


export default {addApi, addNewApi, addTokenToHeader}
